package bb.lanxing.lib.devices.sprint.ymodem;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileYmodemInputStream extends YModemInputStream {
    private File mFile;
    private InputStream mInputStream;

    public FileYmodemInputStream(File file) throws IOException {
        this.mFile = file;
    }

    public FileYmodemInputStream(String str) throws IOException {
        this(new File(str));
    }

    @Override
    public String getFileName() {
        File file = this.mFile;
        if (file != null) {
            return file.getName();
        }
        return null;
    }

    @Override
    public long getTotalSize() {
        File file = this.mFile;
        if (file != null) {
            return file.length();
        }
        return 0L;
    }

    @Override
    public int read() throws IOException {
        if (this.mFile != null && this.mInputStream == null) {
            this.mInputStream = new FileInputStream(this.mFile);
        }
        InputStream inputStream = this.mInputStream;
        if (inputStream != null) {
            return inputStream.read();
        }
        return -1;
    }

    @Override
    public synchronized void reset() throws IOException {
        super.reset();
        this.mInputStream = null;
    }

    @Override
    public void close() throws IOException {
        InputStream inputStream = this.mInputStream;
        if (inputStream != null) {
            inputStream.close();
        }
    }
}
